﻿@model Dictionary<string, object>
<div class="pageContent">
    <div class="panelBar">
        <ul class="toolBar">
            <li><a id="__btnAjaxUploadFile" href="javascript:viod(0)" class="add"><span>上传</span></a></li>
            <li class="line"></li>
        </ul>
    </div>
    <div id="__AjaxUploadedFiles">
        <div>已上传文件：</div>
        <ul></ul>
    </div>
    <div>
    @{
        StringBuilder tips = new StringBuilder();
        tips.AppendFormat("提示：只能上传 {0} 格式的文件", Model["filetypes"]);
        int maxCount = Convert.ToInt32(Model["maxcount"]);
        if (maxCount > 0)
        {
            tips.AppendFormat("，最多可上传 {0} 个文件", maxCount.ToString());
        } 
        int maxSize = Convert.ToInt32(Model["maxsize"]);
        if (maxSize > 0)
        {
            tips.AppendFormat("，单个文件的大小不得超过 {0} KB", maxSize.ToString());
        }
        tips.Append("。");
        @tips
    }
    </div>
    <div class="formBar">
        <ul>
            <li>
                <div class="buttonActive">
                    <div class="buttonContent"><button type="button" onclick="completeAjaxFileUpload()">确定</button></div>
                </div>
            </li>
        </ul>
    </div>
    <script src="/scripts/ajaxUpload-3.6.js" type="text/javascript"></script>
    <script type="text/javascript">
        var _AjaxUploadedFileCount = 0;
        var _UploadedFileNames = '';
        var _UploadedFileUrls = '';
        $(function () {
            new AjaxUpload('#__btnAjaxUploadFile', {
                action: '/File/AjaxUpload',
                data: { 'id':'@Model["No"]' },
                responseType: 'json',
                onSubmit: function (file, ext) {
                    if ((@Model["maxcount"] > 0) && (_AjaxUploadedFileCount >= @Model["maxcount"])) {
                        alert('最多只能上传 @Model["maxcount"] 个文件！');

                        return false;
                    }

                    if (_UploadedFileNames.indexOf(file + ',') > -1) {
                        alert('该文件已上传！');

                        return false;
                    }

                    $('<li id="__liUploadedFile_' + _AjaxUploadedFileCount + '"></li>').prependTo('#__AjaxUploadedFiles ul');
                    var li = $('#__AjaxUploadedFiles ul li:eq(0)');
                    if (ext && /^(@Model["filetypes"].ToString().Replace(".", string.Empty).Replace(',', '|'))$/.test(ext)) {
                        li.text('正在上传： ' + file);
                    } else {
                        li.remove();
                        alert('只能上传 @Model["filetypes"] 格式的文件！');

                        return false;
                    }
                },
                onComplete: function (file, response) {
                    var li = $('#__AjaxUploadedFiles ul li:eq(0)');
                    if(response.msg.length > 0) {
                        li.remove();
                        alert('上传文件出错：' + response.msg);

                        return;
                    }
                    _AjaxUploadedFileCount ++;
                    li.text(_AjaxUploadedFileCount + '、');
                    li.append('<span id="__UploadedFile_' + _AjaxUploadedFileCount + '">' + file + '</span>');
                    //li.append('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:cancelAjaxUploadedFile(' + _AjaxUploadedFileCount + ')">删除</a>');
                    if(_UploadedFileNames.length > 0) {
                        _UploadedFileNames += ',';
                        _UploadedFileUrls += ',';
                    }
                    _UploadedFileNames += file;
                    _UploadedFileUrls += response.url;
                }
            });
        });
        function cancelAjaxUploadedFile(id) {
            _UploadedFileNames.replace($('#__UploadedFile_' + id).text(), '');
            $('#__liUploadedFile_' + id).remove();
        }
        function completeAjaxFileUpload() {
            $.pdialog.closeCurrent();
            @{
                object cb = Model["Callback"];
                if((cb != null) && (cb.ToString().Length > 0))
                {
                    @Html.Raw(string.Format("if(typeof {0} != 'undefined') {{ {0}(_UploadedFileNames, _UploadedFileUrls);}}", cb));
                }
            }
        }
    </script>
</div>